package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.mapper.*;
import cn.wolfcode.crm.query.SaleItemChartQueryObject;
import cn.wolfcode.crm.service.ISmartAnalysisService;
import cn.wolfcode.crm.util.ProductSaleItemObject;
import cn.wolfcode.crm.util.SaleItemChartJson;
import cn.wolfcode.crm.web.util.DailyChartJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;


@Service
public class SmartAnalysisServiceImpl implements ISmartAnalysisService {

    @Autowired
    private SaleAccountMapper saleAccountMapper;

    @Autowired
    private StockOutcomeBillMapper stcoutMapper;

    @Autowired
    private RechargeBillMapper rechargeBillMapper;

    @Autowired
    private KindItemMapper kindItemMapper;


    public SaleItemChartJson saleItemChart(SaleItemChartQueryObject qo) {
        SaleItemChartJson result = new SaleItemChartJson();
        List<ProductSaleItemObject> list = saleAccountMapper.saleItemChart(qo);
        result.setRows(list);
        result.setTotal(list.size());
        return result;
    }

    public DailyChartJson dailyChart() {
        DailyChartJson result = new DailyChartJson();

        //支付总额,按支付方式
        List<BigDecimal> list = stcoutMapper.selectTotalMoneyOrderByPaykind();

        result.setCash(list.size() > 0?list.get(0):new BigDecimal("0.00"));
        result.setRechargecard(list.size() > 1 ?list.get(1):new BigDecimal("0.00"));
        result.setWechat(list.size() > 2?list.get(2):new BigDecimal("0.00"));
        result.setAlipay(list.size() > 3?list.get(3):new BigDecimal("0.00"));
        result.setBankCard(list.size() > 4?list.get(4):new BigDecimal("0.00"));
        //支付总额,按客源
        result.setMemberTotalMoney(stcoutMapper.selectMemberTotalMoney()!=null?stcoutMapper.selectMemberTotalMoney():new BigDecimal("0.00"));
        result.setNotmemberTotalMoney(stcoutMapper.selectNotMemberTotalMoney()!=null?stcoutMapper.selectNotMemberTotalMoney():new BigDecimal("0.00"));

        //营业额,按时间段
        result.setMorning(stcoutMapper.selectMorning());
        result.setAfternoon(stcoutMapper.selectAfternoon());
        result.setNight(stcoutMapper.selectNight());

        //消费次数
        result.setMemberTimes(stcoutMapper.selectMemberTimes());
        result.setNotmemberTimes(stcoutMapper.selectNotMemberTimes());

        //总营业额
        BigDecimal saleTotalAmount = stcoutMapper.selectSaleTotalAmount();
        result.setTotalSaleAmount(saleTotalAmount);
        //总折扣金额
        BigDecimal totalDiscountMoney = stcoutMapper.selectTotalDiscountMoney();
        result.setTotalDiscountAmount(totalDiscountMoney!=null?totalDiscountMoney:new BigDecimal("0.00"));
        //总赠送金额
        BigDecimal totalGiftAmount = rechargeBillMapper.selectTotalGiftAmount();
        result.setTotalGiftAmount(totalGiftAmount!=null?totalGiftAmount:new BigDecimal("0.00"));
        //门店支出
        BigDecimal dailyExpense = kindItemMapper.selectDailyExpense();
        result.setExpenses(dailyExpense!=null?dailyExpense:new BigDecimal("0.00"));
        //门店结余
        if (totalDiscountMoney==null){
            totalDiscountMoney = new BigDecimal("0");
        }
        if (totalGiftAmount==null){
            totalGiftAmount = new BigDecimal("0");
        }
        if (dailyExpense==null){
            dailyExpense = new BigDecimal("0");
        }
        result.setSurplus(saleTotalAmount.subtract(totalDiscountMoney.add(totalGiftAmount.add(dailyExpense))));

        return result;
    }


}
